package com.ssy.lingxi.common.constant.marketing;

import com.ssy.lingxi.common.language.LanguageHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 活动类型: 1-特价促销 2-直降促销 3-折扣促销 4-满量促销 5-满额促销
 *         6-赠送促销 7-多件促销 8-组合促销 9-拼团 10-抽奖
 *         11-砍价 12-秒杀 13-换购 14-预售 15-套餐 16-试用
 *
 * @author fhj
 * @date 2021/09/10
 * @version 2.0.0
 */
public enum ActivityTypeEnum {

    SPECIAL_OFFER(1, "特价促销", "4,5,6,13"),
    PLUMMET(2, "直降促销", "4,5,6,13"),
    DISCOUNT(3, "折扣促销 ", "4,5,6,13"),
    FULL_QUANTITY(4, "满量促销", "1,2,3,6,13"),
    FULL_MONEY(5, "满额促销", "1,2,3,6,13"),
    GIVE(6, "赠送促销", "1,2,3,4,5,7,8,13"),
    MORE_PIECE(7, "多件促销", "6,13"),
    COMBINATION(8, "组合促销", "6,13"),
    GROUP_PURCHASE(9, "拼团", ""),
    LOTTERY(10, "抽奖", ""),
    BARGAIN(11, "砍价", ""),
    SEC_KILL(12, "秒杀", ""),
    SWAP(13, "换购", "1,2,3,4,5,6,7,8"),
    PRE_SALE(14, "预售", ""),
    SET_MEAL(15, "套餐", ""),
    ATTEMPT(16, "试用", ""),
    ;

    // 活动类型
    private final Integer code;

    // 活动类型名称
    private final String message;

    // 允许叠加活动类型
    private final String superpositionItem;

    ActivityTypeEnum(Integer code, String message, String superpositionItem) {
        this.code = code;
        this.message = message;
        this.superpositionItem = superpositionItem;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return LanguageHolder.getName(this.getClass(), this.code);
    }

    public String getSuperpositionItem() {
        return superpositionItem;
    }

    /**
     * 根据code获取对应message
     * @param code 活动类型
     */
    public static String getMessage(Integer code) {
        return Arrays.stream(ActivityTypeEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().map(ActivityTypeEnum::getMessage).orElse("");
    }

    /**
     * 根据code获取对应枚举
     * @param code 活动类型
     */
    public static ActivityTypeEnum findByCode(Integer code) {
        return Arrays.stream(ActivityTypeEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
    }

    /**
     * 判断是否包含附属信息(赠送促销、换购、套餐)
     * @param activityType 活动类型
     */
    public static boolean containSubsidiary(Integer activityType) {
        return Stream.of(GIVE, SWAP, SET_MEAL).anyMatch(o -> o.getCode().equals(activityType));
    }

    /**
     * 判断是否包含附属信息(赠送促销、换购、套餐)
     * @param activityType 活动类型
     */
    public static boolean containSwapOrSetMeal(Integer activityType) {
        return Stream.of(SWAP, SET_MEAL).anyMatch(o -> o.getCode().equals(activityType));
    }

    /**
     * 判断是否可以叠加指定的活动
     * @param activityType 活动类型
     * @param items 允许叠加活动类型
     */
    public static boolean isValidSuperpositionItem(Integer activityType, List<Integer> items) {
        ActivityTypeEnum byCode = findByCode(activityType);
        if (byCode != null) {
            String[] split = byCode.getSuperpositionItem().split(",");
            if (split.length > 0) {
                // 包含叠加活动的类型
                List<Integer> collect = new ArrayList<>();
                for (String s : split) {
                    collect.add(Integer.parseInt(s));
                }
                Integer superpositionType = items.stream().filter(i ->
                        !collect.contains(i)
                ).findFirst().orElse(null);
                return superpositionType == null;
            } else {
                // 不含叠加活动的类型
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 获取与该活动类型互斥的活动类型
     * @param activityType 活动类型
     * */
    public static List<Integer> getMutualExclusive(Integer activityType) {
        if (activityType.equals(SPECIAL_OFFER.code) || activityType.equals(PLUMMET.code) || activityType.equals(DISCOUNT.code)) {
            return Stream.of(SPECIAL_OFFER.code, PLUMMET.code, DISCOUNT.code).collect(Collectors.toList());
        } else if (activityType.equals(FULL_QUANTITY.code) || activityType.equals(FULL_MONEY.code)) {
            return Stream.of(FULL_QUANTITY.code, FULL_MONEY.code).collect(Collectors.toList());
        }

        return Collections.emptyList();
    }

    /**
     * 校验该活动类型是否存在互斥
     * @param activityType 活动类型
     * */
    public static Boolean checkMutualExclusive(Integer activityType) {
        // 存在互斥的活动
        List<Integer> list = Stream.of(
                SPECIAL_OFFER.code,
                PLUMMET.code,
                DISCOUNT.code,
                FULL_QUANTITY.code,
                FULL_MONEY.code
        ).collect(Collectors.toList());
        // 是否包含
        return list.contains(activityType);
    }


    /**
     * 校验该活动类型能否叠加其他活动
     * @param activityType 活动类型
     * */
    public static Boolean checkFold(Integer activityType) {
        // 可叠加的活动
        List<Integer> list = Stream.of(
                SPECIAL_OFFER.code,
                PLUMMET.code,
                DISCOUNT.code,
                FULL_QUANTITY.code,
                FULL_MONEY.code,
                GIVE.code,
                MORE_PIECE.code,
                COMBINATION.code,
                SWAP.code
        ).collect(Collectors.toList());
        // 是否包含
        return list.contains(activityType);
    }

    /**
     * 判断是否为单价维度的活动：特价、直降、折扣
     * @param activityType 活动类型
     * */
    public static Boolean isPriceSaleActivityType(Integer activityType) {
        return activityType.equals(SPECIAL_OFFER.code) || activityType.equals(PLUMMET.code) || activityType.equals(DISCOUNT.code);
    }
}